package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.EquipmentCleaningHistory;

public class EquipmentCleaningHistoryPresenter extends AbstractPresenter<EquipmentCleaningHistory> {
	final String SINGLEITEM = "equipmentCleaningHistory";
	final String MULTIITEM = "equipmentCleaningHistories";
	
	public ArrayList<EquipmentCleaningHistory> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public EquipmentCleaningHistory tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public EquipmentCleaningHistory createItemFromJSONObject(JSONObject jsonObject){
		EquipmentCleaningHistory item = new EquipmentCleaningHistory();
		try{
			item.setEcID(jsonObject.getInt("ecid"));
			item.setEcDate(getDateFromString(jsonObject.getString("ECDate")));
			
			if(!jsonObject.isNull("ECEmpID")){
				//create product if set
				EmployeePresenter presenter = new EmployeePresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("ECEmpID");
				item.setEcEmployeeID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setEcEmployeeID(null);
			}
			
			if(!jsonObject.isNull("ECProcedureID")){
				//create product if set
				VesselCleaningProcedurePresenter presenter = new VesselCleaningProcedurePresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("ECProcedureID");
				item.setEcProcedureID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setEcProcedureID(null);
			}
			
			if(!jsonObject.isNull("ECVesselID")){
				//create product if set
				EquipmentPresenter presenter = new EquipmentPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("ECVesselID");
				item.setEcVesselID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setEcVesselID(null);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(EquipmentCleaningHistory item){
		try{
			JSONObject json = new JSONObject();
			json.put("ecid", item.getEcID());
			json.put("ECDate", getDate(item.getEcDate()));

			if(item.getEcEmployeeID()!=null){
				final EmployeePresenter presenter = new EmployeePresenter();
				json.put("ECEmpID", presenter.createJSON(item.getEcEmployeeID()));
			} else {
				json.put("ECEmpID", JSONObject.NULL);
			}

			if(item.getEcProcedureID()!=null){
				final VesselCleaningProcedurePresenter presenter = new VesselCleaningProcedurePresenter();
				json.put("ECProcedureID", presenter.createJSON(item.getEcProcedureID()));
			} else {
				json.put("ECProcedureID", JSONObject.NULL);
			}

			if(item.getEcVesselID()!=null){
				final EquipmentPresenter presenter = new EquipmentPresenter();
				json.put("ECVesselID", presenter.createJSON(item.getEcVesselID()));
			} else {
				json.put("ECVesselID", JSONObject.NULL);
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public EquipmentCleaningHistory getEmptyEntity(){
		EquipmentCleaningHistory entity = new EquipmentCleaningHistory();
		return entity;
	}
}
